﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace ShapeDraw.Server
{
    public class ServiceHelper
    {
        public static string ServiceUrl = "http://localhost:8083/WebService/CommService.svc";
        public static Service.CommServiceClient InitService()
        {
            Service.CommServiceClient client = new Service.CommServiceClient();
            client.Endpoint.Address = new System.ServiceModel.EndpointAddress(ServiceUrl);
            return client;

        }



        public static class WcfServiceClientFactory<TServiceClient, TService>
            where TServiceClient : ClientBase<TService>, TService
            where TService : class
        {
            public static TServiceClient CreateServiceClient()
            {
                var typeName = typeof(TService).Name;
                var serviceAddress = "../" + typeName + ".svc";
                return CreateServiceClient(serviceAddress);
            }

            public static TServiceClient CreateServiceClient(string serviceAddress)
            {
                var endpointAddr = new EndpointAddress(new Uri(Application.Current.Host.Source, serviceAddress));
                var binding = new BasicHttpBinding();
                var ctor = typeof(TServiceClient).GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
                return (TServiceClient)ctor.Invoke(new object[] { binding, endpointAddr });
            }
        }
    }
}
